package me.skyun.androidarch.connect;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by skyun on 17/8/25.
 */

public class TcpServer extends ConnServer<ServerSocket, Socket> {

    private int mPort;

    public TcpServer(int port, Callback serverCallback, Connection.Callback connectionCallback) {
        super(serverCallback, connectionCallback);
        mPort = port;
    }

    @Override
    protected ServerSocket createListenSocket() {
        if (mListenSocket == null) {
            try {
                mListenSocket = new ServerSocket(mPort);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mListenSocket;
    }

    @Override
    protected Socket accept() throws IOException {
        return mListenSocket.accept();
    }

    @Override
    protected String getAddress(Socket conSock) {
        return conSock.getRemoteSocketAddress().toString();
    }
}
